// pages/index/pay.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        radio: 1,
        cardId: 0
    },
    ycf: function(e) {
        this.setData({
            radio: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        this.setData({
            cardId: decodeURIComponent(options.cardId)
        })
        wx.login({
            success: function(res) {
                if (res.code) {
                    console.log(res.code)
                    that.setData({
                        'code': res.code
                    })
                    //发起网络请求
                    wx.request({
                        url: 'https://bnk.xiutub.com/',
                        data: {
                            code: res.code
                        }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
    view_moneysure: function() {
        var code = this.data.code;
        var openId = wx.getStorageSync('openId');
        //console.log(this.data.radio);
        if (!this.data.radio) {
            wx.showToast({
                title: '请选择支付方式'
            });
        } else {
            console.log('code是' + code)
            console.log('openId是' + openId)
            wx.request({
                url: 'https://bnk.xiutub.com/home/pay/index', //这个链接是后端写的
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                    radio: this.data.radio,
                    cardId: this.data.cardId,
                    code: code,
                    openId: openId
                },
                method: 'POST',
                success: function(response) {
                    console.log(response.data);
                    // 发起支付
                    wx.requestPayment({
                        'appId': response.data.appId,
                        'timeStamp': response.data.timeStamp,
                        'nonceStr': response.data.nonceStr,
                        'package': response.data.package,
                        'signType': 'MD5',
                        'paySign': response.data.paySign,
                        'success': function(res) {
                            wx.showToast({
                                title: '支付成功'
                            });

                            wx.navigateTo({
                                url: '/pages/index/index'
                            });
                            console.log(res);
                        },
                        'fail': function(res) {
                            console.log(res)
                        }
                    });
                },
                fail: function(res) {
                    console.log(res)
                }
            })
        }
    },
})